{$CLEO .cs}
{$USE CLEO+}
{$USE Audio}
{$USE newOpcodes}
{$USE ini}

0000: NOP

script_name "Music from cars"

// By ArtemQa146

const
    MusicActor = 0@
    MusicCar = 31@
    AudioMusic = 30@
    VolumeCar = 29@
    Find = 28@
    Current_Station = 27@
    FM_1 = 26@
    FM_2 = 25@
    FM_3 = 24@
    FM_4 = 23@
    FM_5 = 22@
    FM_6 = 21@
    FM_7 = 20@
    FM_8 = 19@
    FM_9 = 18@
    FM_10 = 17@
    Pause_Day = 16@
    Pause_Night = 15@
    Music = 10@
end

0AF2: VolumeCar = read_float_from_ini_file "cleo\Music from cars.ini" section "Settings" key "Volume"
0AF0: Pause_Day = read_int_from_ini_file "cleo\Music from cars.ini" section "Settings" key "Pause Time Day"
0AF0: Pause_Night = read_int_from_ini_file "cleo\Music from cars.ini" section "Settings" key "Pause Time Night"
0AF0: FM_1 = read_int_from_ini_file "cleo\Music from cars.ini" section "Radio ID Music" key "Radio ID 1"
0AF0: FM_2 = read_int_from_ini_file "cleo\Music from cars.ini" section "Radio ID Music" key "Radio ID 2"
0AF0: FM_3 = read_int_from_ini_file "cleo\Music from cars.ini" section "Radio ID Music" key "Radio ID 3"
0AF0: FM_4 = read_int_from_ini_file "cleo\Music from cars.ini" section "Radio ID Music" key "Radio ID 4"
0AF0: FM_5 = read_int_from_ini_file "cleo\Music from cars.ini" section "Radio ID Music" key "Radio ID 5"
0AF0: FM_6 = read_int_from_ini_file "cleo\Music from cars.ini" section "Radio ID Music" key "Radio ID 6"
0AF0: FM_7 = read_int_from_ini_file "cleo\Music from cars.ini" section "Radio ID Music" key "Radio ID 7"
0AF0: FM_8 = read_int_from_ini_file "cleo\Music from cars.ini" section "Radio ID Music" key "Radio ID 8"
0AF0: FM_9 = read_int_from_ini_file "cleo\Music from cars.ini" section "Radio ID Music" key "Radio ID 9"
0AF0: FM_10 = read_int_from_ini_file "cleo\Music from cars.ini" section "Radio ID Music" key "Radio ID 10"
FM_1 += 1
FM_2 += 1
FM_3 += 1
FM_4 += 1
FM_5 += 1
FM_6 += 1
FM_7 += 1
FM_8 += 1
FM_9 += 1
FM_10 += 1

Pause_Day *= 1000
Pause_Night *= 1000

while true
wait 0
    if
        Player.Defined($PLAYER_CHAR)
    then
    
        if
            timera > 5000
        then
            0E40: get_current_hour 1@
            if and
                1@ > 9
                1@ < 21
            then
                wait Pause_Day
            else
                wait Pause_Night
            end
            timera = 0
        end
    
        Find = 0
        while 0EA7: get_any_char_no_save_recursive Find progress_to Find char_to MusicActor
            if and
                00DF:   actor MusicActor driving
                not is_char_script_controlled MusicActor
                not locate_char_distance_to_char $PLAYER_ACTOR char MusicActor radius 40.0
            then
                089F: get_actor MusicActor pedtype_to 1@
                if or
                    1@ == 4
                    1@ == 5
                then
                    MusicCar = Actor.CurrentCar(MusicActor)
                    0E12: get_vehicle MusicCar subclass_to 1@
                    if
                        1@ == 0
                    then
                        gosub @Music

                        //    
                        repeat
                        wait 0
                        if
                            056E:  car MusicCar defined
                        then
                            //   ,    3       ,   
                            0407: store_coords_to 1@ 2@ 3@ from_car MusicCar with_offset 0.0 0.0 0.0
                            0EB2: get_offset_from_camera_in_world_coords 0.0 0.0 0.0 store_to 4@ 5@ 6@
                            050A: 1@ = distance_between_XYZ 1@ 2@ 3@ and_XYZ 4@ 5@ 6@
                            1@ /= 100.0
                            0087: 2@ = VolumeCar //   .ini 
                            0063: 2@ -= 1@  // (float)
                                if
                                    2@ < 0.0
                                then
                                    2@ = 0.0
                                end
                            0ABC: set_audio_stream AudioMusic volume 2@

                            //   
                            0AB9: get_audio_stream AudioMusic state_to 1@
                            if
                                1@ == -1
                            then
                                gosub @Music
                            end

                            //  -   
                            if or
                                0119:   car MusicCar wrecked
                                00DB:   actor $PLAYER_ACTOR in_car MusicCar
                                0E08: is_car_script_controlled MusicCar
                                8ABE:   not car MusicCar engine_on
                            then
                                Current_Station -= 1 //     0
                                if 00DB:   actor $PLAYER_ACTOR in_car MusicCar
                                then 041E: set_radio_station Current_Station
                                end
                                MusicCar = -1
                                break
                            end
                        end
                        until 856E:  not car MusicCar defined
                        0AAE: remove_audio_stream AudioMusic
                    end
                end
            end
        end
    end
end


:Music
0209: Current_Station = random_int_in_ranges 1 11
if  Current_Station == 1
then 0085: Music = FM_1
end
if  Current_Station == 2
then 0085: Music = FM_2
end
if  Current_Station == 3
then 0085: Music = FM_3
end
if  Current_Station == 4
then 0085: Music = FM_4
end
if  Current_Station == 5
then 0085: Music = FM_5
end
if  Current_Station == 6
then 0085: Music = FM_6
end
if  Current_Station == 7
then 0085: Music = FM_7
end
if  Current_Station == 8
then 0085: Music = FM_8
end
if  Current_Station == 9
then 0085: Music = FM_9
end
if  Current_Station == 10
then 0085: Music = FM_10
end
0209: 1@ = random_int_in_ranges 1 Music
0AD3: 5@v = string_format "QA\%d_(%d).mp3" Current_Station 1@ 
0AC1: AudioMusic = load_audio_stream_with_3d_support 5@v
0AC5: link_3d_audio_stream AudioMusic to_car MusicCar
0AAD: set_audio_stream AudioMusic state 1
return

